package com.ideaaedi.plugin.fastcoding.ui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ideaaedi.commonds.exception.ExceptionUtil;
import com.ideaaedi.commonds.quick.ClipboardUtil;
import com.ideaaedi.plugin.fastcoding.action.MockJsonAction;
import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.ideaaedi.plugin.fastcoding.util.JdPsiClassUtil;
import com.ideaaedi.plugin.fastcoding.util.NotifyUtil;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiType;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * json框
 *
 * @author JustryDeng
 * @since 2022/1/6 22:55:47
 */
public class MockJson_ShowJson extends JFrame {
    public JPanel jPanel;
    public JTextArea jsonArea;
    private JButton copyButton;
    private JButton formatButton;
    private JButton copyAndCloseButton;
    
    public MockJson_ShowJson(Project project, String className) {
        // 设置内容面板
        setContentPane(jPanel);
        // 设置标题
        setTitle(FastCodingConstant.PLUGIN_NAME + FastCodingConstant.CONCAT_SIGN_WITH_WHITESPACE + FastCodingConstant.MOCK_JSON_NAME +
                FastCodingConstant.ADVANCE_SIGN_WITH_WHITESPACE + className);
        // 设置默认高亮的按钮
        getRootPane().setDefaultButton(copyAndCloseButton);
    
    
        // 设置大小 和 位置
        setSize(1000, 700);
        setLocationRelativeTo(null);
        
        
        // 添加监听器
        initListener(project);
    }
    
    // 添加监听器
    private void initListener(Project project) {
        // 格式化
        formatButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String jsonText = jsonArea.getText();
                try {
                    String formatJsonText = JSON.toJSONString(JSON.parse(jsonText, Feature.OrderedField), SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect);
                    jsonArea.setText(formatJsonText);
                } catch (Exception ex) {
                    NotifyUtil.notify(project, MockJsonAction.ACTION_NAME, ExceptionUtil.getStackTraceMessage(ex), NotificationType.ERROR);
                }
            }
        });
        // 复制
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String jsonText = jsonArea.getText();
                    ClipboardUtil.setClipboardString(jsonText);
                } catch (Exception ex) {
                    NotifyUtil.notify(project, MockJsonAction.ACTION_NAME, ExceptionUtil.getStackTraceMessage(ex), NotificationType.ERROR);
                }
            }
        });
        
        
        // 复制并关闭
        copyAndCloseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String jsonText = jsonArea.getText();
                    ClipboardUtil.setClipboardString(jsonText);
                    dispose();
                } catch (Exception ex) {
                    NotifyUtil.notify(project, MockJsonAction.ACTION_NAME, ExceptionUtil.getStackTraceMessage(ex), NotificationType.ERROR);
                }
            }
        });
    }
    
    /**
     * 展示json
     */
    public static void invokeShowJson(Project project, PsiType psiType) {
        String mockedJson = JSON.toJSONString(JdPsiClassUtil.mockClass(project, psiType), SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect);
        MockJson_ShowJson showJson = new MockJson_ShowJson(project, psiType.getPresentableText());
        showJson.jsonArea.setText(mockedJson);
        // 关闭当前选择框
        showJson.dispose();
        // 弹出json框
        showJson.setVisible(true);
    }
}
